#ifndef _UDPSOCKET_H_
#define _UDPSOCKET_H_

#include <iostream>
#include <string>
#include <cstring>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>

class UdpServer
{
private:
    int m_serverfd;
    int m_port;

public:

    UdpServer(int port):m_serverfd(-1), m_port(port)
    {
        createSocket();
        bindSocket();
    }
    //初始化套接字
    void createSocket();
    /* 绑定 */
    void bindSocket();
    /* 连接 */
    void acceptConnection();
    /*  */
    void start();

    ~UdpServer()
    {
        if(m_serverfd != -1)
        {
            close(m_serverfd);
        }
    }
};

class UdpClient
{
private:
    int m_sockfd;
    int m_serverPort;
    std::string m_serverIP;

public:
    UdpClient(const std::string& serverIP, int  serverPort)
    :m_sockfd(-1),m_serverIP(serverIP), m_serverPort(serverPort)
    {
        connectToServer();
    }
    void connectToServer();
    void sendMessage(const std::string& message);
    

    ~UdpClient()
    {
        if(m_sockfd != -1)
        {
            close(m_sockfd);
        }
    }

};

#endif